﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eSilky.VO;
using System.Data;

namespace eSilky.Admin.Usercontrols
    {
    public partial class uctlGroupManagement : System.Web.UI.UserControl
        {

        protected void Page_Load(object sender, EventArgs e)
            {
            BindGroup();
            }

        protected void btnSave_Click(object sender, EventArgs e)
            {
            GroupMaster objGroup = new GroupMaster();
            objGroup.name = txt_Grp_GroupName.Text.Trim();
            objGroup.description = txt_Grp_GroupDescription.Text.Trim();


            if (btnSave.Text == "Save")
                {
                if (new DAL.Group().SaveGroup(objGroup))
                    {
                    lblMsg.Text = "Saved Successfully";
                    }
                }
            else if (btnSave.Text == "Update")
                {
                string grpId = hdnGrpId.Value.Trim() != "" ? hdnGrpId.Value.Trim() : "0";
                objGroup.id = Convert.ToInt32(grpId);

                if (new DAL.Group().UpdateGroup(objGroup))
                    {
                    lblMsg.Text = "Updated Successfully";
                    }
                }
            }

        private void BindGroup()
            {

            try
                {

                DataTable dtGroup = new DAL.Group().GetGroups();

                if (dtGroup != null && dtGroup.Rows.Count > 0)
                    {
                    gvGroup.DataSource = dtGroup;
                    gvGroup.DataBind();
                    dvGrid.Style["display"] = "block";
                    dvForm.Style["display"] = "none";
                    }
                else
                    {
                    dvGrid.Style["display"] = "none";
                    dvForm.Style["display"] = "block";
                    }
                }
            catch (Exception ex)
                {
                throw ex;
                }
            }

        protected void gvGroup_RowCommand(object sender, GridViewCommandEventArgs e)
            {
            if (e.CommandName == "EditGroup")
                {
                btnSave.Text = "Update";
                hdnGrpId.Value = e.CommandArgument.ToString();
                }
            else if (e.CommandName == "DeleteGroup")
                {
                 if(new DAL.Group().DeleteGroup(Convert.ToInt32(e.CommandArgument)))
                     {
                      BindGroup();
                     }
                }
            }


        }
    }